//获取点赞消息
function getLikesMessage() {
    axios.get('/message/likes?uid='+$("#uid").val())
        .then(function (response) {
            var str =""
            $.each(response.data.data,function (index,item) {
                str += ' <fieldset class="layui-elem-field layui-field-title">\n' +
                    '        <legend><span>&nbsp;&nbsp;<a style="text-decoration: none;color: grey;font-size: 12px" href="/home/user/index/'+item.sid+'"><b>'+item.name1+'</b>&nbsp;在&nbsp;</a><span style="font-size: 8px"><a href="/home/post/index/'+item.link+'">'+item.content+'</a>&nbsp;中赞了你</span></span><span>&nbsp;<button class="btn btn-primary btn-xs" id="'+item.id+'" onclick="deleteMessageLike(this.id)">知道啦</button></span></legend>\n' +
                    '    </fieldset>';
            });
            $("#likes").html(str);
            $('#message_new').html($("#message").html());
        })
        .catch(function (error) {
            console.log(error);
        });
}
//获取关注消息
function getCollectionMessage() {
    axios.get('/message/collection?uid='+UID)
        .then(function (response) {
            var str ="";
            $.each(response.data.data,function (index,item) {
                str +='<fieldset class="layui-elem-field layui-field-title" v-for="item in collection">\n' +
                    '            <legend><span>&nbsp;&nbsp;<a style="text-decoration: none;color: grey;font-size: 12px"  href="/home/user/index/'+item.sid+'"><b>'+item.name1+'</b>&nbsp;&nbsp;</a><span style="font-size: 8px">&nbsp;关注了你</span></span><span>&nbsp;<button class="btn btn-primary btn-xs" id="'+item.id+'" onclick="deleteMessageCollection(this.id)">知道啦</button></span></legend>\n' +
                    '        </fieldset>';
            });
            $("#collection").html(str);
            $('#message_new').html($("#message").html());
        })
        .catch(function (error) {
            console.log(error);
        });

}
//获取评论与回复消息
function getCommentMessage() {
    axios.get('/message/comment?uid='+UID)
        .then(function (response) {
            var str ="";
            $.each(response.data.data,function (index,item) {
                str +='  <fieldset class="layui-elem-field layui-field-title" v-for="item in comment">\n' +
                    '            <legend><span>&nbsp;&nbsp;<a style="text-decoration: none;color: grey;font-size: 12px"  href="/home/user/index/'+item.sid+'"><b>'+item.name1+'</b>&nbsp;在&nbsp;</a><span style="font-size: 8px"><a href="/home/post/index/'+item.link+'">文章</a>&nbsp;中'+item.type+'了你:'+item.content+'</span></span><span>&nbsp;<button class="btn btn-primary btn-xs" id="'+item.id+'" onclick="deleteMessageComment(this.id)">知道啦</button></span><span>&nbsp;<a class="btn btn-primary btn-xs" href="/home/post/index/'+item.link+'">前往回复</a></span></legend>\n' +
                    '        </fieldset>';
            });
            $("#comment").html(str);
            $('#message_new').html($("#message").html());
        })
        .catch(function (error) {
            console.log(error);
        });
}
//获取通知消息总数
function getMessageNum(){
    var nums = 0;
    axios.get('/message/likes?uid='+UID)
        .then(function (response) {
            nums += response.data.data.length;
            axios.get('/message/collection?uid='+$("#uid").val())
                .then(function (response) {
                    nums += response.data.data.length;
                    axios.get('/message/comment?uid='+$("#uid").val())
                        .then(function (response) {
                            nums += response.data.data.length;
                            $("#messagenums").html(nums);
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                })
                .catch(function (error) {
                    console.log(error);
                });
        })
        .catch(function (error) {
            console.log(error);
        });
}

//展示消息框
function showMessage(){
    layui.use('layer',function () {
        layer.tab({
                skin: 'layui-layer-moon', //样式类名
                area: ['600px', '350px'],
                tab: [{
                    title: '消息通知',
                    content: "<div id='message_new'>"+$('#message').html()+"</div>"
                }]
            });
    })
}

//改变消息状态为已读
function deleteMessageLike(id) {
    layui.use('layer', function() {
        var index = layer.load(2);
        axios.put('/message/likes/update/status?id='+id)
            .then(function (reponse) {
                layer.msg("已读");
                getMessageNum();
                getLikesMessage();
                layer.close(index);
            })
            .catch(function (error) {
                layer.close(index);
            });
    });
}
function deleteMessageComment(id) {
    layui.use('layer', function() {
        var index = layer.load(2);
        axios.put('/message/comment/update/status?id='+id)
            .then(function (reponse) {
                getMessageNum();
                getCommentMessage();
                layer.msg("已读");
                layer.close(index);
            })
            .catch(function (error) {
                layer.close(index);
            });
    });
}
function deleteMessageCollection(id) {
    layui.use('layer', function() {
        var index = layer.load(2);
        axios.put('/message/collection/update/status?id='+id)
            .then(function (reponse) {
                getMessageNum();
                getCollectionMessage();
                layer.msg("已读");
                layer.close(index);
            })
            .catch(function (error) {
                layer.close(index);
            });
    });
}
getLikesMessage();
getCollectionMessage();
getCommentMessage();
getMessageNum();
